//
//  MLVisitorLoginView.swift
//  SinaWeibo
//
//  Created by 李亮亮 on 16/4/6.
//  Copyright © 2016年 Big Fish. All rights reserved.
//

import UIKit
import SnapKit

// 声明协议   NSObjectProtocol 会有weak
@objc protocol MLVisitorLoginViewDelegate: NSObjectProtocol {
    // 声明方法
    optional func userWillLogin()
    optional func userWillRegister()
    
}


class MLVisitorLoginView: UIView {

    // 声明代理对象 需要考虑循环引用
    // swift 中属性默认的修饰, 就是strong
    weak var visitorViewDelegate: MLVisitorLoginViewDelegate?
    
    // 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //
        setupUI()
     
    }
    
    //
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 对外提供设置界面的方法
    // 设置参数的默认值 参数可以不传
    func setVisitorInfo(tipText: String, imageName: String, isHome: Bool = false) {
        
        if isHome {
            // 开始动画
            animationStart()
        } else {
            // 隐藏largeHouse
            largeHouse.hidden = true
            // 将circleIcon置于上层
            bringSubviewToFront(circleIcon)
        }
        // 重新赋值
        tipLabel.text = tipText
        circleIcon.image = UIImage(named: imageName)
        
    }
    
    
    private func animationStart() {
    
        // 基本动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        // 角度
        animation.toValue = 2 * M_PI
        // 时间
        animation.duration = 15
        // 次数
        animation.repeatCount = MAXFLOAT
        // 动画执行结束 或者 当前处于非活跃状态下  动画不移除图层
        animation.removedOnCompletion = false
        // 添加动画到图片的图层上
        circleIcon.layer.addAnimation(animation, forKey: nil)
    
    }
    
    
    // MARK: - 设置UI界面
    private func setupUI() {
        
        // 添加子控件
        addSubview(circleIcon)
        addSubview(backImage)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)

        // VFL
        
        // 将约束对象添加到父控件上
        // 手写代码添加约束, 默认frame布局还是开启, 需要关闭frame布局
        // 自动布局计算公式
        //  view1.attr1 = view2.attr2 * multiplier + constant
//        circleIcon.translatesAutoresizingMaskIntoConstraints = false
//        self.addConstraint( NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        self.addConstraint( NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))

        
        // 自动布局
        circleIcon.snp_makeConstraints { (make) in
            //make 就是circleIcon 取消了frame布局的对象
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        backImage.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon)
        }
        
        largeHouse.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon)
        }
        
        tipLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(loginBtn.snp_top)
            make.width.equalTo(loginBtn)
        }

        // 设置背景色   sRGB取色
        let value: CGFloat = 237 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
        
        // 添加按钮的点击事件
        loginBtn.addTarget(self, action: #selector(MLVisitorLoginView.loginBtnDidClick), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(MLVisitorLoginView.registerBtnDidClick), forControlEvents: .TouchUpInside)

    }
    

    // MARK: - 按钮的监听事件
    @objc private func loginBtnDidClick() {
        print("login")
        // 使用代理对象  调用协议方法
        // 1.判断代理对象是否不为 nil   2.判断代理对象是否响应协议方法
        visitorViewDelegate?.userWillLogin?()
    }
    
    @objc private func registerBtnDidClick() {
        print("register")
        // 1.判断代理对象是否不为 nil   2.判断代理对象是否响应协议方法
        visitorViewDelegate?.userWillRegister?()
    }
    
    
    // MARK: - 懒加载子视图
    // 遮罩图片
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 圆圈图片
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 中部房子图片
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    // 提示文案
    private lazy var tipLabel: UILabel = UILabel(text: "", textColor: UIColor.darkGrayColor(), fontSize: 16)

    // 登录按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return btn
    }()
    
    // 注册按钮
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)

        return btn
    }()

}
